<template>
	<view class="home-view view-bg">
		<NavBar title="推荐"></NavBar>
		<view class="banner">
			<swiper class="swiper" indicator-dots circular autoplay :interval="3000" :duration="1000">
				<swiper-item class="swiper-item" v-for="item in bannerList" :key="item._id	">
					<image :src="item.picurl" mode="aspectFill"></image>
				</swiper-item>
			</swiper>
		</view>
		<div class="notice">
			<view class="left">
				<uni-icons type="sound-filled" size="20"></uni-icons>
				<text class="text">公告</text>
			</view>
			<div class="middle">
				<swiper vertical autoplay :interval="3000" :duration="500">
					<swiper-item v-for="item in noticeList" :key="item._id" @click="noticeNav(item)">
						关于我们，相关介绍
					</swiper-item>
				</swiper>
			</div>
			<div class="right">
				<uni-icons type="right" size="20"></uni-icons>
			</div>
		</div>
		<view class="recom-box">
			<CommonTitle text="每日推荐" :isCalendar="true" />
			<div class="content">
				<scroll-view scroll-x>
					<view v-for="item in randomList" :key="item._id" class="box">
						<image :src="item.smallPicurl" mode="aspectFill" @click="onNavgatior(item)"></image>
					</view>
				</scroll-view>
			</div>
		</view>
		<view class="theme-box">
			<CommonTitle text="专题精选" />
			<div class="theme-content">
				<ThemeItem v-for="item in selectedTopicsList" :key="item._id" :item="item" />
				<ThemeItem :isMore="true" />
			</div>
		</view>
	</view>
</template>

<script setup>
	import {
		getBanner,
		getNotice,
		getRandomList,
		getSelectedTopics
	} from '@/api/requestInterface.js'
	import {
		useWallListStore
	} from '@/stores/wallList.js'

	const wallList = useWallListStore()

	const bannerList = ref([])
	const noticeList = ref([])
	const randomList = ref([])
	const selectedTopicsList = ref([])

	// 获取banner数据
	const loadBannerData = async () => {
		const {
			errCode,
			data
		} = await getBanner()
		if (errCode === 0) {
			bannerList.value = data
		} else {
			bannerList.value = []
		}
	}

	// 获取公告内容
	const loadNoticeData = async () => {
		const {
			errCode,
			data
		} = await getNotice()
		if (errCode === 0) {
			noticeList.value = data
		} else {
			noticeList.value = []
		}
	}

	// 获取每日推荐壁纸
	const loadRandomData = async () => {
		const {
			errCode,
			data
		} = await getRandomList()

		if (errCode === 0) {
			randomList.value = data
		} else {
			randomList.value = []
		}
	}

	// 获取专题精选壁纸
	const loadSelectedTopics = async () => {
		const {
			errCode,
			data
		} = await getSelectedTopics({
			select: true
		})

		if (errCode === 0) {
			selectedTopicsList.value = data
		} else {
			selectedTopicsList.value = []
		}
	}

	// 初始化页面数据
	const initData = () => {
		loadBannerData()
		loadNoticeData()
		loadRandomData()
		loadSelectedTopics()
	}

	initData()

	// 公告详情跳转
	const noticeNav = (item) => {
		uni.navigateTo({
			url: '/pages/noticeDetail/noticeDetail?id=' + item._id
		})
	}

	// 页面跳转
	const onNavgatior = (item) => {
		wallList.updateData(randomList.value)
		uni.navigateTo({
			url: `/pages/preview/preview?id=${item._id}`
		})
	}

	// 分享
	onShareAppMessage((e) => {
		return {
			title: '壁纸',
			path: '/pages/index/index'
		}
	})

	// 分享朋友圈
	onShareTimeline(() => {
		return {
			title: '壁纸'
		}
	})
</script>

<style lang="scss" scoped>
	.home-view {
		.banner {
			width: 750rpx;
			padding: 30rpx 0;

			.swiper {
				width: 750rpx;
				height: 340rpx;
				box-sizing: border-box;

				.swiper-item {
					width: 100%;
					height: 100%;
					padding: 0 30rpx;
					box-sizing: border-box;

					image {
						width: 100%;
						height: 100%;
					}
				}
			}
		}

		.notice {
			width: 690rpx;
			height: 80rpx;
			background-color: #f9f9f9;
			margin: 0 auto;
			box-sizing: border-box;
			border-radius: 80rpx;
			display: flex;
			line-height: 80rpx;

			.left {
				width: 140rpx;
				display: flex;
				justify-content: center;
				align-items: center;

				.text {
					color: $brand-theme-color;
					font-size: 28rpx;
				}

				:deep() {

					.uni-icons {
						color: $brand-theme-color !important;
					}
				}
			}

			.middle {
				flex: 1;

				swiper {
					height: 100%;

					&-item {
						height: 100%;
						font-size: 30rpx;
						color: #666;
						overflow: hidden;
						white-space: nowrap;
						text-overflow: ellipsis;
					}
				}
			}

			.right {
				width: 70rpx;
				display: flex;
				justify-content: center;
				align-items: center;
			}
		}

		.recom-box {
			padding-top: 50rpx;

			.content {
				width: 720rpx;
				margin-left: 30rpx;
				margin-top: 30rpx;

				scroll-view {
					white-space: nowrap;

					.box {
						width: 200rpx;
						height: 450rpx;
						display: inline-block;
						margin-right: 15rpx;

						image {
							width: 100%;
							height: 100%;
						}
					}

					.box:last-child {
						margin-right: 30rpx;
					}

				}
			}
		}

		.theme-box {
			padding: 50rpx 0;

			.theme-content {
				margin-top: 30rpx;
				padding: 0 30rpx;
				display: grid;
				gap: 15rpx;
				grid-template-columns: repeat(3, 1fr);
			}
		}
	}
</style>